//
//  BBRepairOneViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/20.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
import SVProgressHUD
import CoreLocation
class BBRepairOneViewController: UIViewController {
    @IBOutlet var selfCheckTV: UITextView!
    var selfCheckCode = "0000000000"
    
    var locationManager: CLLocationManager?
    override func viewDidLoad() {
        super.viewDidLoad()
        getLocationAuth()
        self.selfCheckTV.isUserInteractionEnabled = false
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: UIBarButtonItem.Style.plain, target: self, action: #selector(nextAction))
    }

    func getLocationAuth() {
        locationManager = CLLocationManager()
        locationManager!.delegate = self
        locationManager!.requestWhenInUseAuthorization()
        
    }
    @objc private func nextAction() {
        if selfCheckTV.text.count == 0 || selfCheckTV.text == "Self-checking information" {
            SVProgressHUD.showError(withStatus: "Self-check information cannot be empty")
            return
        }
        let vc = BBRepairTwoViewController.init(nibName: "BBRepairTwoViewController", bundle: nil)
        vc.device_check_info = selfCheckCode
        self.navigationController!.pushViewController(vc, animated: true)
    }

    @IBAction func checkAciton(_ sender: Any) {
        let acon = UIAlertController(title: "Manual detection", message: "No Bluetooth connection, please manually detect failure", preferredStyle: UIAlertController.Style.alert)
        let actOk = UIAlertAction(title: "Sure", style: UIAlertAction.Style.default) { _ in
            let str: String = (acon.textFields?.first?.text)!
            if str.count == 0 {
                return
            }
            let code = Int(str)
            switch code {
            case 1:
                self.selfCheckTV.text = "Current anomaly"
                self.selfCheckCode = "1000000000"
                break
            case 2:
                self.selfCheckTV.text = "Turn handle abnormity"
                self.selfCheckCode = "0100000000"
                break
            case 3:
                self.selfCheckTV.text = "Motor phase deficiency"
                 self.selfCheckCode = "0010000000"
                break
            case 4:
                self.selfCheckTV.text = "Hall anomaly"
                 self.selfCheckCode = "0001000000"
                break
            case 5:
                self.selfCheckTV.text = "Brake abnormity"
                 self.selfCheckCode = "0000100000"
                break
            case 6:
                self.selfCheckTV.text = "Undervoltage"
                 self.selfCheckCode = "0000010000"
                break
            case 7:
                self.selfCheckTV.text = "Motor clogging"
                self.selfCheckCode = "0000001000"
                break
            case 8:
                self.selfCheckTV.text = "Controller communication receiving exception"
                self.selfCheckCode = "0000000100"
                break
            case 9:
                self.selfCheckTV.text = "Instrument communication receiving anomaly"
                self.selfCheckCode = "0000000010"
                break
            default:
                self.selfCheckTV.text = "No abnormality"
                 self.selfCheckCode = "0000000000"
                break
            }
        }
        let actCancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) { _ in
        }
        acon.addTextField { text in
            text.placeholder = "Input instrument display fault code"
        }
        acon.addAction(actOk)
        acon.addAction(actCancel)
        present(acon, animated: true, completion: nil)
    }
}
extension BBRepairOneViewController: CLLocationManagerDelegate {
    
}
